﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NBFModel;
using PWMIS.DataMap.Entity;
using NBFCommon;

namespace NBFBLL
{
    public  class ChangePassWordBLL
    {
        /// <summary>
        /// 更新密码
        /// </summary>
        /// <param name="productnumber">用户序列号</param>
        /// <param name="oldpassword">老密码</param>
        /// <param name="password">新密码</param>
        /// <param name="other">保留字段</param>
        /// <returns>修改结果</returns>
        public string SetPassWord(string productnumber,string oldpassword,string password,string other)
        {
            //if (oldpassword.Length != 0)
            //{
            //    //EntityQuery<Model_tb_ManagePwd>.Update(
            //}
            //else
            //{
            //    Model_tb_ManagePwd mtm = new Model_tb_ManagePwd();
            //    mtm.id = 1001;
            //    mtm.productNumber = "";
            //    mtm.UserPwd = NBF_Config.getMd5(oldpassword);
            //    mtm.SetPwdDate = DateTime.Now.ToString("yyyy-MM-dd");
            //    OQL  o = new OQL(mtm);
            //    OQLCompare oc = new OQLCompare(mtm);
            //    o.Delete().Where(oc.Comparer(mtm.id, ">", 0));
            //    EntityQuery<Model_tb_ManagePwd>.ExecuteOql(o);

            //    if(EntityQuery<Model_tb_ManagePwd>.Insert(mtm) > 0)
            //    {
            //        return "添加成功！";
            //    }
            //}
            Model_tb_ManagePwd mtm = new Model_tb_ManagePwd();
            mtm.productNumber = productnumber;           
            OQL o = new OQL(mtm);
            o.Select().Where(o.Condition.AND(mtm.productNumber,"=",productnumber));

            List<Model_tb_ManagePwd> mtms = EntityQuery<Model_tb_ManagePwd>.QueryList(o);
            if (mtms.Count > 0)
            {
                Model_tb_ManagePwd mtmnew = mtms[0];
                if (mtmnew.UserPwd == NBF_Config.getMd5(oldpassword))
                {
                    OQL updateoql = new OQL(mtmnew);
                    mtmnew.UserPwd = NBF_Config.getMd5(password);
                    mtmnew.SetPwdDate = DateTime.Now.ToString("yyyy-MM-dd");
                    updateoql.Update().Where(mtmnew.productNumber);
                    int nums = EntityQuery<Model_tb_ManagePwd>.Instance.ExecuteOql(updateoql);
                    if (nums == 0) return "修改失败";
                    else return "";
                }
                else
                    return "老密码错误！";
            }
            else
                return "不存在该用户";
            
        }
    }
}
